package www.dianbaozu.com.ui.login

import androidx.databinding.ObservableField
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import www.dianbaozu.com.R
import www.dianbaozu.com.extension.nimFlow
import www.dianbaozu.com.extension.save
import www.wen.com.common.lib.base.module.BaseCountDownModel
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.util.SpUtil


/**
 * @author WEN
 * @Description:注册页面ViewModel
 * @date 2020//25 17:48
 */
class RegisteViewModel @ViewModelInject constructor(appDataManager: AppDataManager) :
    BaseCountDownModel(appDataManager) {

    val mAgree = ObservableField(false)

    /**
     * 验证码注册|登陆
     */
    @FlowPreview
    fun requestRegister(
        requestRegisterBody: RequestRegisterBody,
        action: (() -> Unit),
    ) {
        viewModelScope.launch {
            appDataManager.loginOrRegisterFlow("register", requestRegisterBody)
                .retryWhen { cause, attempt ->
                    return@retryWhen cause.message == "网络异常" && attempt <= 1
                }
                .flatMapConcat {

                    appDataManager.nimFlow(
                            it.data.user.accId,
                            it.data.user.accToken
                    ) {

                        SpUtil.save(it.data.user)
                        SpUtil.save(it.data.token!!)
                        SpUtil.encode(KeyHelper.MMKV_IS_FIRST_LOGIN,true)
                    }
                }
                .onStart {
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            getString(R.string.logging_in),
                            LoadingDialogStateType.LOADING
                        )
                    )
                }
                .onCompletion { cause ->
                    if (cause == null) {
                        loading.value = UiStateCustom(
                            stateData = LoadingDialog(
                                tip = getString(R.string.successful_landing),
                                loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                isShowDialogTip = false,
                                isShowToastTip = true
                            )
                        )
                    }
                }
                .catch { cause ->
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            tip = cause.message ?: "网络异常",
                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                            isShowDialogTip = false,
                            isShowToastTip = true
                        )
                    )
                }
                .collect {
                    action.invoke()
                }
        }
    }

    override fun switchEnabledSubmit(textChange: CharSequence) {
        mEnabledSubmit.value = true

//        mEnabledSubmit.value = mPhone.value!!.isNotBlank() &&
//                mPhone.value!!.length >= 5 &&
//                mVerificationCode.value!!.length >= 4 &&
//                mPassWord.value!!.length >= 6 &&
//                mAgree.get()!!
    }
}


